package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.msg.WmNewsDownOrUpMsg;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ArticleKafkaListener {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = {WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC})
    public void downOrUp(String msg){
        //1.将消息反序列化为对象
        if(StringUtils.isBlank(msg)){
        log.error("消息为空");
        return;
        }
        WmNewsDownOrUpMsg wmNewsDownOrUpMsg = JSON.parseObject(msg, WmNewsDownOrUpMsg.class);

        //更新配置表
        apArticleConfigMapper.update(null, Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId,wmNewsDownOrUpMsg.getArticleId())
                .set(ApArticleConfig::getIsDown,wmNewsDownOrUpMsg.getEnable() == 1 ? 0 : 1)
        );
    }
}
